package exerciseFrames;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import person.PersonRecorder;
import avatarFrame.AvatarInfoPanel;
import constants.Constants;
import engine.AvartaEngine;
import exerciseRecord.ArmRecord;
import exerciseRecord.Exercise;
import exerciseRecord.ExerciseRecorder;
import exerciseRecord.LegRecord;

public class LegPanel extends JPanel{
	private JLabel weightLabel_front, weightLabel_back, countLabel_front, countLabel_back, caloriLabel_front, caloriLabel_back;
	private JTextField weightField, countField, caloriField;
	private AvatarInfoPanel avatarInfoPanel;
	private ExercisePanel exercisePanel;
	private JButton exerciseButton, homeButton;

	private EventHandler eventHandler;

	public LegPanel() {
		this.setLayout(null);
		this.setSize(Constants.PANEL_SIZE_WIDTH, Constants.PANEL_SIZE_HEIGHT);
		this.setBackground(Constants.PANEL_BACKGROUND_COLOR);

		// speed components
		weightLabel_front = new JLabel("Weight :");
		weightLabel_front.setBounds(50, 75, 55, 20);
		this.add(weightLabel_front);

		weightField = new JTextField();
		weightField.setBounds(100, 75, 50, 20);
		this.add(weightField);

		weightLabel_back = new JLabel("kg");
		weightLabel_back.setBounds(155, 75, 100, 20);
		this.add(weightLabel_back);

		// time components
		countLabel_front = new JLabel("Count :");
		countLabel_front.setBounds(50, 125, 55, 20);
		this.add(countLabel_front);

		countField = new JTextField();
		countField.setBounds(100, 125, 50, 20);
		this.add(countField);

		countLabel_back = new JLabel("EA");
		countLabel_back.setBounds(155, 125, 100, 20);
		this.add(countLabel_back);

		// calori components
		caloriLabel_front = new JLabel("Calori :");
		caloriLabel_front.setBounds(50, 200, 55, 20);
		this.add(caloriLabel_front);

		caloriField = new JTextField();
		caloriField.setBounds(100, 200, 50, 20);
		this.add(caloriField);

		caloriLabel_back = new JLabel("kcal");
		caloriLabel_back.setBounds(155, 200, 100, 20);
		this.add(caloriLabel_back);

		// event handler
		eventHandler = new EventHandler();

		// setUp button
		exerciseButton = new JButton(new ImageIcon(Constants.APPLY_IMG));
		exerciseButton.setBounds(224, 232, 50, 50);
		exerciseButton.setActionCommand("exercise");
		exerciseButton.addActionListener(eventHandler);
		this.add(exerciseButton);

		// home button
		homeButton = new JButton(new ImageIcon(Constants.PREV_IMG));
		homeButton.setBounds(30, 250, 30, 30);
		homeButton.setActionCommand("prev");
		homeButton.addActionListener(eventHandler);
		this.add(homeButton);

	}

	public void paint(Graphics g) {
		super.paint(g);

		g.drawImage(Constants.EXERCISE_PANEL_IMG, 0, 20, this);
	}

	public void init(ExercisePanel exercisePanel) {
		this.exercisePanel = exercisePanel;
		this.avatarInfoPanel = exercisePanel.getMainFrame().getAvartaFrame().getAvatarInfoPanel();
	}
	
	public int getCalori() {
		return Integer.parseInt(caloriField.getText());
	}
	
	public void clear() {
		weightField.setText("");
		countField.setText("");
	}

	private class EventHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand() == "exercise") {
				PersonRecorder personRecorder = exercisePanel.getPersonRecorder();
				ArrayList<ExerciseRecorder> exerciseRecorderArray = personRecorder.getExerciseRecorderArray();
				ExerciseRecorder latestRecorder = exerciseRecorderArray.get(exerciseRecorderArray.size()-1);

				Exercise legRecord = new LegRecord();
				
				int weight = Integer.parseInt(weightField.getText());
				int count = Integer.parseInt(countField.getText());
				int starImgCount = AvartaEngine.getAvartaEngine().getLegRate(weight, count);
				avatarInfoPanel.setLegMuscleRate(starImgCount);
				avatarInfoPanel.addConsumedCalori(getCalori());
				
				legRecord.setWeight(weight);
				legRecord.setCount(count);

				latestRecorder.getExerciseArray().add(legRecord);
			} else if (e.getActionCommand() == "prev") {
				exercisePanel.setExercisePanel();
			}
		}

	}
}
